package com.bolovan.infovaluta.bean;

import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.bolovan.infovaluta.util.CurrencyDescriptionMap;
import com.bolovan.infovaluta.util.RateConstants;
import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class ExchangeRateEntry implements Comparable<ExchangeRateEntry> {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private String currency;
	@Persistent
	private BigDecimal bid;
	@Persistent
	private BigDecimal ask;
	@Persistent
	private Integer multiplier = 1;
	@Persistent
	private ExchangeRateSeller seller;
	@Persistent
	private String date;
	@Persistent
	private String parsingDate;
	@NotPersistent
	private String currencyDescr;

	public ExchangeRateEntry(String currency, BigDecimal bid, BigDecimal ask) {
		this(currency, bid, ask, null);
	}

	public ExchangeRateEntry(String currency, BigDecimal bid, BigDecimal ask, Integer multiplier) {
		this.currency = currency;
		this.currencyDescr = CurrencyDescriptionMap.currencyDescr.get(currency);
		if (bid != null) {
			this.bid = bid.setScale(6, RoundingMode.CEILING);
		}
		if (ask != null) {
			this.ask = ask.setScale(6, RoundingMode.CEILING);
		}
		if (multiplier != null && multiplier > 1) {
			this.multiplier = multiplier;
		}
	}

	public String getCurrency() {
		return currency;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}

	public BigDecimal getBid() {
		return bid;
	}

	public void setBid(BigDecimal bid) {
		this.bid = bid;
	}

	public BigDecimal getAsk() {
		return ask;
	}

	public void setAsk(BigDecimal ask) {
		this.ask = ask;
	}

	public Integer getMultiplier() {
		return multiplier;
	}

	public void setMultiplier(Integer multiplier) {
		this.multiplier = multiplier;
	}

	public ExchangeRateSeller getSeller() {
		return seller;
	}

	public void setSeller(ExchangeRateSeller seller) {
		this.seller = seller;
	}

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getParsingDate() {
		return parsingDate;
	}

	public void setParsingDate(String parsingDate) {
		this.parsingDate = parsingDate;
	}

	public String getCurrencyDescr() {
		return currencyDescr;
	}

	public void setCurrencyDescr(String currencyDescr) {
		this.currencyDescr = currencyDescr;
	}

	@Override
	public String toString() {
		return "ExchangeRateEntry [key=" + key + ", currency=" + currency + ", bid=" + bid + ", ask=" + ask + ", multiplier=" + multiplier
				+ ", seller=" + seller + ", date=" + date + ", parsingDate=" + parsingDate + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((ask == null) ? 0 : ask.hashCode());
		result = prime * result + ((bid == null) ? 0 : bid.hashCode());
		result = prime * result + ((currency == null) ? 0 : currency.hashCode());
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + ((key == null) ? 0 : key.hashCode());
		result = prime * result + ((multiplier == null) ? 0 : multiplier.hashCode());
		result = prime * result + ((parsingDate == null) ? 0 : parsingDate.hashCode());
		result = prime * result + ((seller == null) ? 0 : seller.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ExchangeRateEntry other = (ExchangeRateEntry) obj;
		if (ask == null) {
			if (other.ask != null)
				return false;
		} else if (!ask.equals(other.ask))
			return false;
		if (bid == null) {
			if (other.bid != null)
				return false;
		} else if (!bid.equals(other.bid))
			return false;
		if (currency == null) {
			if (other.currency != null)
				return false;
		} else if (!currency.equals(other.currency))
			return false;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (key == null) {
			if (other.key != null)
				return false;
		} else if (!key.equals(other.key))
			return false;
		if (multiplier == null) {
			if (other.multiplier != null)
				return false;
		} else if (!multiplier.equals(other.multiplier))
			return false;
		if (parsingDate == null) {
			if (other.parsingDate != null)
				return false;
		} else if (!parsingDate.equals(other.parsingDate))
			return false;
		if (seller != other.seller)
			return false;
		return true;
	}

	@Override
	public int compareTo(ExchangeRateEntry o) {
		if (o.getCurrency() == this.getCurrency()) {
			return 0;
		} else if (RateConstants.prefferedCurrencies.contains(o.getCurrency())) {
			return 1;
		} else {
			return -1;
		}
	}

}